from rest_framework.authtoken.models import Token
from apps.common.base.serializers import *
from apps.user.models import *
from apps.education.student.serializers import StudentBaseSerializer
from apps.education.teacher.serializers import TeacherBaseSerializer

class TokenBaseSerializer(serializers.ModelSerializer):

    class Meta:
        model = Token
        fields = '__all__'

class TokenModelSerializer(TokenBaseSerializer):
    class Meta:
        model = Token
        fields = '__all__'

class UserBaseSerializer(BaseSerializer):
    username = serializers.CharField(required=False, allow_blank=True, allow_null=True)
    password = serializers.CharField(required=False, allow_blank=True, allow_null=True)
    class Meta:
        model = UserModel
        fields = "__all__"

# 管理端用户列表
class UserModelSerializer(BaseSerializer):

    infoModel = serializers.SerializerMethodField()
    def get_infoModel(self, obj):
        if obj.role.key == 'student':
            return StudentBaseSerializer(obj.user_student.first()).data
        elif obj.role.key == 'teacher':
            return TeacherBaseSerializer(obj.user_teacher.first()).data
        else:
            return {}
        
    class Meta:
        model = UserModel
        exclude = ['password']


# 管理员列表
class AdminModelSerializer(BaseSerializer):


    roleModel = serializers.SerializerMethodField()
    def get_roleModel(self, obj):
        try:
            return {'id': obj.role.id, 'name': obj.role.name, 'key': obj.role.key}
        except:
            return {}

    class Meta:
        model = UserModel
        fields = ('id', 'username', 'realname', 'nickname', 'phone', 'sort', 'createTime', 'role', 'roleModel',
                  'status')